package org.test.workutil.uipsql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.test.util.MyCollectionUtil;

public class TMD {
	
	//insert into TD_S_BIZLIST (BIZ_CODE, BIZ_NAME, RECK_TAG, SECURITY_TAG) values ('BOPO003', '统一支付平台', '', '');
	
	//insert into TD_S_TRANSLIST (TRANS_CODE, INTF_CODE, TRANS_NAME, TIME_OUT, REQ_CHAN_ID, RSP_CHAN_ID, TRANS_TYPE)
	//values ('TOPI004', 'OI_SendShortMessageES', '短信下发短厅版', '', '', '', '');
	/**
	 * 生成MAP {BISO002=[TISO001, TISO002], BISO001=[TISO001, TISO002, TISO003]} 这种
	 * @param bizTransMap 为[]
	 * @param bizTranList biz和tran对应 [BISO001:TISO001,TISO002,TISO003, BISO002:TISO001,TISO002]
	 */
	private static void genbizTranMap(Map<String, List<String>> bizTransMap,List<String> bizTranList){
//		System.out.println(bizTransMap);
//		System.out.println(bizTranList);
//		try {
//		Thread.sleep(10000000);
//	} catch (InterruptedException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//	}
		
		for (String oneBizTran : bizTranList) {
			int index = oneBizTran.lastIndexOf(':');
			String bizcode = oneBizTran.substring(0, index);
			List<String> list =  MyCollectionUtil.converStringToList(oneBizTran.substring(index+1), ",");
			bizTransMap.put(bizcode, list);
		}
	
	}

	public static void main(String[] args) {
		Map<String, List<String>> bizTransMap = new HashMap<String, List<String>>();
		
		Map<String,String> transInterfaceMap = new HashMap<String, String>();
		
		transInterfaceMap.put("TSPO095", "OI_CheckIfSpecNum");
		

//		System.out.println(transInterfaceMap);
		
		String bizTranStr1 = "BOPS015:TSPO095";
		List<String> bizTranList = new ArrayList<String>();
		bizTranList.add(bizTranStr1);
		
		genbizTranMap(bizTransMap, bizTranList);
		
		genSqlByMap(bizTransMap,transInterfaceMap);
	}
	
	/**
	 * 生成sql并打印，
	 * @param bizTransMap 形如 {BISO002=[TISO001, TISO002], BISO001=[TISO001, TISO002, TISO003]}
	 * @param transInterfaceMap tran 和 接口对应 形如 {TISO003=OI_TISO003, TISO001=OI_TISO001, TISO002=OI_TISO002}
	 */
	private static void genSqlByMap(Map<String, List<String>> bizTransMap,Map<String,String> transInterfaceMap){
		
		List<String> TD_S_BIZTRANS = new ArrayList<String>();
		
		String TD_S_BIZTRANS_sql = "insert into uop_uec.TD_S_BIZTRANS (BIZ_CODE, TRANS_CODE, INTF_CODE, " +
					"RELATION_TYPE, TRANS_TYPE, DEPEND_CLASS, OPER_STAFF_ID, OPER_TIME)values (";
		
		
		List<String> TD_S_STAFF_BIZLIST = new ArrayList<String>();
		String TD_S_STAFF_BIZLIST_sql = "insert into uop_uec.TD_S_STAFF_BIZLIST " +
				"(BIZ_CODE, TRANS_CODE, ID_TYPE, STAFF_ROLE, CHAN_ID, FUNC_DESC, STATUS, START_DATE, END_DATE, OPER_STAFF_ID, OPER_TIME) values(";
		List<String> roleStrList = new ArrayList<String>();
		roleStrList.add("SMSPROLE");
		roleStrList.add("SELFROLE");
		roleStrList.add("WEBPROLE");
		roleStrList.add("WAPPROLE");
		roleStrList.add("GROPROLE");
		roleStrList.add("UTILROLE");
		roleStrList.add("USSDROLE");
		
		Set<String> keySet =bizTransMap.keySet();
		//业务
		for (String bizStr : keySet) {
//			String tmpStr = new String(sql);
//			tmpStr+="'"+bizStr+"',";
			List<String> trans = bizTransMap.get(bizStr);
			//业务里的接口
			for (String transtr : trans) {
				String tmpsqlStr = new String(TD_S_BIZTRANS_sql);
				tmpsqlStr+="'"+bizStr+"',";
				tmpsqlStr+="'"+transtr+"',";
				tmpsqlStr+="'"+transInterfaceMap.get(transtr)+"',";
				tmpsqlStr+="'0', '0', '0', 'SUPERUSR', sysdate);";
				TD_S_BIZTRANS.add(tmpsqlStr);
				
				//加权限表
				for (String roleStr : roleStrList) {
					String tmpRoleSql = new String(TD_S_STAFF_BIZLIST_sql);
					tmpRoleSql+="'"+bizStr+"',";
					tmpRoleSql+="'"+transtr+"','1',";
					tmpRoleSql+="'"+roleStr+"',";
					tmpRoleSql+="'ZZZZ', '', '0', to_date('12-12-2012', 'dd-mm-yyyy'), to_date('01-01-2050', 'dd-mm-yyyy'), 'SUPERUSR', to_date('12-12-2012 10:41:00', 'dd-mm-yyyy hh24:mi:ss'));";
					TD_S_STAFF_BIZLIST.add(tmpRoleSql);
				}
				TD_S_STAFF_BIZLIST.add("");
			}
		}
		
		
		System.out.println("--三、TD_S_BIZTRANS中间表:");
		for (String t : TD_S_BIZTRANS) {
			System.out.println(t);
		}
		
		System.out.println("--四、TD_S_STAFF_BIZLIST权限表:");
		for (String s : TD_S_STAFF_BIZLIST) {
			System.out.println(s);
		}
		
	}
}
